import {
    setStore,
    getStore,
    removeStore,
} from '@/util/store';
import {
    validatenull,
} from '@/util/validate';
import {
    baseUrl,
} from '@/config/env';
import website from '@/const/website';
import { getLocalCode } from '@/api/common';

const common = {

    state: {
        minHeight: document.documentElement.clientHeight,
        localCode: getStore({
            name: 'localCode',
        }) || {},
        isCollapse: false,
        isFullScren: false,
        isLock: getStore({
            name: 'isLock',
        }) || false,
        theme: getStore({
            name: 'theme',
        }) || '#409EFF',
        lockPasswd: getStore({
            name: 'lockPasswd',
        }) || '',
        website,
    },
    actions: {
        GetLocalCode({
            commit,
            state,
            dispatch,
            rootState,
        }, params) {
            return new Promise((resolve, reject) => {
                const userInfo = rootState.user.userInfo;
                getLocalCode(userInfo.communityId, userInfo.buildingId, userInfo.unitId, 2).then((res) => {
                    const data = res.data.data;
                    commit('SET_LOCAL_CODEL', data);
                });
            });
        },

    },
    mutations: {
        SET_LOCAL_CODEL: (state, localCode) => {
            state.localCode = localCode;
            setStore({
                name: 'localCode',
                content: state.localCode,
            });
        },
        SET_TABBAR: (state, action) => {
            state.isCollapse = !state.isCollapse;
        },
        SET_FULLSCREN: (state, action) => {
            state.isFullScren = !state.isFullScren;
        },
        SET_LOCK: (state, action) => {
            state.isLock = true;
            setStore({
                name: 'isLock',
                content: state.isLock,
                type: 'session',
            });
        },
        SET_THEME: (state, color) => {
            state.theme = color;
            setStore({
                name: 'theme',
                content: state.theme,
            });
        },
        SET_LOCK_PASSWD: (state, lockPasswd) => {
            state.lockPasswd = lockPasswd;
            setStore({
                name: 'lockPasswd',
                content: state.lockPasswd,
                type: 'session',
            });
        },
        CLEAR_LOCK: (state, action) => {
            state.isLock = false;
            state.lockPasswd = '';
            removeStore({
                name: 'lockPasswd',
            });
            removeStore({
                name: 'isLock',
            });
        },
    },
};
export default common;